package com.qk.aspect;

import com.qk.entity.OperateLog;
import com.qk.mapper.OperateLogMapper;
import com.qk.utils.CurrentUserHoler;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class AspectLog {
    @Autowired
    private OperateLogMapper operateLogMapper;
    @Around("@annotation(com.qk.anno.Log)")
    public Object recordLog(ProceedingJoinPoint pjp) throws Throwable {
        log.info("开始执行方法：{}",pjp.getSignature().getName());
        OperateLog operateLog = new OperateLog();
        operateLog.setOperateUserId(CurrentUserHoler.getCurrentUser());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(pjp.getClass().getName());
        operateLog.setMethodName(pjp.getSignature().getName());
        operateLog.setMethodParams(Arrays.toString(pjp.getArgs()));
        long startTime = System.currentTimeMillis();

        Object proceed = pjp.proceed();

        operateLog.setReturnValue(proceed.toString());
        long endTime = System.currentTimeMillis();
        operateLog.setCostTime(endTime - startTime);
        operateLogMapper.insert(operateLog);
        return proceed;
    }
}
